<template>
  <div style="padding: 10px">
    <div style="margin: 10px 0">
      <el-button type="primary" style="margin-right: 10px;" @click="add">新增</el-button>
      <el-popconfirm  class="ml-5" confirm-button-text='好的' cancel-button-text='不用了'
        icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="delBath">
        <el-button plain style="margin-right: 10px;" class="el-icon-circle-close" type="danger" slot="reference">批量删除</el-button>
      </el-popconfirm>
      <el-input v-model="search" placeholder="搜索用户名" style="width: 20%" clearable @clear="reply"></el-input>
      <el-button type="primary" style="margin-left: 10px" @click="loading">查询</el-button>
    </div>
    <el-table :data="tableData" border stripe style="width: calc(98%)" max-height="630">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column sortable prop="id" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="nickName" label="昵称"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="角色">
        <template slot-scope="scope">
          <span v-if="scope.row.role === 1">管理员</span>
          <span v-if="scope.row.role === 2">教师</span>
          <span v-if="scope.row.role === 3">学生</span>
        </template>
      </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px"
            :src="scope.row.portrait ? scope.row.portrait : require('@/assets/defaultPortrait.png')"
            :preview-src-list="[scope.row.portrait ? scope.row.portrait : require('@/assets/defaultPortrait.png')]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">

          <el-button @click="update(scope.row)" size="mini">编辑 </el-button>
          <el-popconfirm title="确定删除吗？" style="margin-left: 5px" @confirm="delate(scope.row.id)">
            <el-button type="danger" size="mini" slot="reference">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[5, 10, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>



    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form label-width="120px" :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.username" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="女">女</el-radio>
          <el-radio v-model="form.sex" label="男">男</el-radio>
        </el-form-item>
        <el-form-item label="地址">
          <el-input type="textarea" v-model="form.address" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <!-- <el-date-picker v-model="form.createTime" clearable type="date" value-format="YYYY-MM-DD" style="width: 80%"></el-date-picker> -->
          <el-upload ref="upload" action="http://localhost:9090/files/upload" :on-success="filesUploadSuccess">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "User",
  data() {
    return {
      form: {},
      total: 0,
      currentPage: 1,
      pageSize: 10,
      search: "",
      dialogVisible: false,
      tableData: [],

    };
  },
  created() {
    this.loading();
  },
  methods: {
    filesUploadSuccess(res) {
      this.form.portrait = res.data

    },

    loading() {
      request
        .get("/user/page", {
          params: {
            pagenum: this.currentPage,
            pagesize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    save() {
      if (this.form.id) {
        request.put("/user", this.form).then((res) => {
          console.log(res);
          if (res.code === "0") {
            this.$message({
              type: "success",
              message: "更新成功",
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
          this.loading();
          this.dialogVisible = false;
        });
      } else {
        request.post("/user", this.form).then((res) => {
          console.log(res);
          if (res.code === "0") {
            this.$message({
              type: "success",
              message: "新增成功",
            });
          } else {
            this.$message({
              type: "error",
              message: "新增失败",
            });
          }
          this.loading();
          this.dialogVisible = false;
        });
      }
    },
    add() {
      this.dialogVisible = true;
      this.form = {};
      this.$nextTick(() => {
        this.$refs.upload.clearFiles();
      })
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.upload.clearFiles();
      })
    },

    delate(id) {
      request.delete("/user/" + id).then((res) => {
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "删除成功",
          });
        } else {
          this.$message({
            type: "error",
            message: "删除失败",
          });
        }
      });
      this.loading();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.loading();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.loading();
    },
    reply() {
      this.loading();
    },
    delBath() {
      // if (!this.ids.length) {
      //   this.$message.warning("请选择数据！")
      //   return
      // }
      // request.delete("/activity/delBatch/" + this.ids).then(res => {
      //   this.$message.success("批量删除成功！");
      //   this.loading()
      // })
    },
  },
};
</script>

<style></style>